<?php
$rs = $this->lastResult;
$when = isEmpty(getParam("desde")) && isEmpty(getParam("hasta")) ? " hasta el día de hoy": " del ".getParam("desde")." al ".getParam("hasta");
$who = isEmpty(getParam('carnet')) ? "" : " del alumno(a) ". $this->stud;
?>

<div style="width:100%; overflow:auto;">
    <input type="hidden" name="page" id="page" />
    <table  style="font-size: 8px" width="100%" border="0" cellspacing="0" class="table table-striped table-bordered table-hover" cellpadding="0">
        <thead>
            <tr>
                <th colspan="14"><legend>Facturas Generadas<?php echo $when.$who; ?></legend></th>
            </tr>
                <tr>
                    <th width="8%" valign="top">Factura</th>
                    <th width="6%" valign="top">Fecha</th>
                    <th width="20%" valign="top">Nombre</th>
                    <th width="20%" valign="top">Concepto</th>
                    <th width="4%" valign="top">Efectivo Quetzales</th>
                    <th width="4%" valign="top">Cheques Bco. BI</th>
                    <th width="4%" valign="top">Cheques Otros Bancos</th>
                    <th width="4%" valign="top">Deposito Directo</th>
                    <th width="4%" valign="top">Dolares</th>
                    <th width="2%" valign="top">No. Boleta </th>
                    <th width="2%" valign="top">No. Cheque </th>
                    <th width="4%" valign="top">Nombre Banco</th>
                    <th width="4%" valign="top">Total Factura Q</th>
                    <th width="6%" valign="top">TIPO CAMBIO</th>
                </tr>
        <tbody>    
            <?php
                
                $union = isEmpty(getParam("carnet")) ? NULL : "SELECT AVG(TASA_CAMBIO) AS PROMEDIO FROM cllg_factura WHERE FK_STUDENT=130408085 AND STATUS!=0";
                
                $totalQ = 0;
                $totalBI = 0;
                $totalOtros = 0;
                $totalTramite = 0;
                $totalEfectivo = 0;
                $totalDeposito = 0;
                $totalDolares = 0;
                $totalFact = 0;
                $tipoCambio = $this->getTasaProm("TASA_CAMBIO","cllg_factura",$union);
                
                //Indexes
                $iBI = 0;
                $iOtros = 0;
                $iTramite = 0;
                $iEfectivo = 0;
                $iDeposito = 0;
                $iDolares = 0;
                $iFact = 0;
 
                foreach ($rs as $r) {
                    //define status 
                    $stat = $r['STATUS'];
                    $iFact++;
                    
                    //define nombre
                    if($stat == 0){
                        $nombre = "<span style='color:red'>ANULADA</span>"; 
                    }elseif($r['STATUS'] == 1 ){
                        $nombre = $r['NOMBRE'];
                    }else{
                        $nombre = "<span style='color:blue'>EN TRÁMITE</span>";
                    }
                    
                    //querys a otras bases de datos
                    $detalle = $this->db->query(sprintf("SELECT * FROM cllg_factura_detalle WHERE cllg_factura_detalle.FK_FACTURA = %s order by ID ASC",$r['ID']));
                    
                    $cheque = $this->db->query(sprintf("SELECT * FROM cllg_cheque WHERE cllg_cheque.FK_FACURA = %s",$r['ID']));
                    $ch = mysql_fetch_assoc($cheque);
                    
                    //GENERACION DE DESCRIPCION PARA PAGOS DE EMPRESA
                    
                    
                ?>
                <tr>
                    <td style="text-align: right"><?php echo $r["NUMERO"] ?></td>
                    <td><?php echo $r["FECHA"] ?></td>
                    <td><?php echo $nombre ?></td>
                    <td>
                    <?php
                        if ($stat == 0){
                            echo "<span style='color:red'>ANULADA</span>";
                        }
                        else{
                            if(isEmpty($r['FK_EMPRESA']) || $r['FK_EMPRESA'] == 0){
                                while($res =  mysql_fetch_assoc($detalle)){                                      
                                        echo $res['DESCRIPCION'];
                                        echo ", ";
                                }
                            }                                                                         
                            else{
                                $empName = $this->db->query_select("cllg_empresa",sprintf("ID='%s'",$r['FK_EMPRESA']));
                                $cargo = $this->db->query_select("cllg_mov_cargo",sprintf("FK_FACTURA='%s'",$r['ID']));
                                
                                echo "PAGO DE EMPRESA ". strtoupper($empName[0]['NOMBRE']) .":<br/> ";
                                
                                foreach ($cargo as $c) {
                                    $det = $this->db->query_select("cllg_cargo",sprintf("ID='%s'",$c['FK_CARGO']));
                                    $acc = $this->db->query_select("cllg_account",sprintf("ID='%s'",$det[0]['FK_ACCOUNT']));
                                    $studName = $this->db->query_select("cllg_student",sprintf("ID='%s'",$acc[0]['FK_STUDENT']));
                                    $studName = $studName[0];
                                    
                                    echo $det[0]['DESCRIPTION']. " del alumno " . $studName['FIRST_NAME'] . " " . $studName['LAST_NAME'] . "<br/>";
                                    
                                }
                            }
                        }
                    ?>
                    </td>
                    <td style="text-align: right"><?php echo ($stat == 0 ? "&nbsp" : ($r['EFECTIVO'] == NULL ? "&nbsp" : ($r['EFECTIVO'] == 0 ? "&nbsp;" : number_format(floatval($r["EFECTIVO"] + $r["TARJETA"]),2)))); ?></td>
                    <td style="text-align: right"><?php echo ($stat == 0 ? "&nbsp" : ($ch['BANCO'] == "BI" ? ($r['CHEQUE'] == 0 ? "&nbsp;" : number_format(floatval($r['CHEQUE']),2)) : "&nbsp"));?></td>
                    <td style="text-align: right"><?php echo ($stat == 0 ? "&nbsp" : ($ch['BANCO'] != "BI" ? ($r['CHEQUE'] == 0 ? "&nbsp;" : number_format(floatval($r['CHEQUE']),2)) : "&nbsp"));?></td>
                    <td style="text-align: right"><?php echo ($stat == 0 ? "&nbsp" : ($r['DEPOSITO'] == NULL ? "&nbsp" : ($r['DEPOSITO'] == 0 ? "&nbsp;" : number_format(floatval($r["DEPOSITO"]),2)))); ?></td>
                    <td style="text-align: right"><?php echo ($stat == 0 ? "&nbsp" : ($r['DOLARES'] == NULL ? "&nbsp" : ($r['DOLARES'] == 0 ? "&nbsp;" : number_format(floatval($r["DOLARES"]),2))));?></td>
                    <td style="text-align: right"><?php echo ($stat == 0 ? "&nbsp" : ($r["NO_BOLETA"] == NULL ? "&nbsp;" : $r['NO_BOLETA'])); ?></td>
                    <td style="text-align: right"><?php echo ($stat == 0 ? "&nbsp" : ($r['CHEQUE'] > 0 ? $ch['NUMERO'] : "&nbsp;"));?></td>
                    <td><?php echo ($stat == 0 ? "&nbsp" : ($r['CHEQUE'] > 0 ? $ch['BANCO'] : "&nbsp;"));?></td>
                    <td style="text-align: right"><?php echo ($stat == 0 ? "&nbsp" : number_format($r["TOTAL"],2)); ?></td>                    
                    <td style="text-align: right"><?php echo ($stat == 0 ? "&nbsp" : ($r["TASA_CAMBIO"] == NULL ? "&nbsp;" : number_format(floatval($r['TASA_CAMBIO']),2)));?></td>
                </tr>
            <?php 
                    if($ch['BANCO'] == "BI"){
                        if($stat != 0){
                        $totalBI += $r['CHEQUE'];
                        }
                    }
                    if($ch['BANCO'] != "BI"){
                        if ($stat != 0){
                        $totalOtros += $r['CHEQUE'];
                        }
                    }
                    if($stat != 0){
                        if($stat == 1 || $stat == NULL){
                            $totalQ += $r['TOTAL'];
                        }
                        else{
                            $totalTramite += $r['TOTAL'];
                        }
                    }
                    $totalEfectivo += $stat == 0 ? 0 : ($r['EFECTIVO'] == NULL ? 0 : $r["EFECTIVO"] + $r["TARJETA"]);
                    $totalDeposito += $stat == 0 ? 0 : ($r['DEPOSITO'] == NULL ? 0 : $r["DEPOSITO"]);
                    $totalDolares += $stat == 0 ? 0 : ($r['DOLARES'] == NULL ? 0 : $r["DOLARES"]);
                    $totalFact += $stat == 0 ? 0 : ($r['TOTAL'] == NULL ? 0 : $r["TOTAL"]);
                    
                    $iEfectivo += $stat == 0 ? 0 : (intval($r['EFECTIVO']) > 0 ? 1 : 0);
                    $iDeposito += $stat == 0 ? 0 : (intval($r['DEPOSITO']) > 0 ? 1 : 0);
                    $iDolares += $stat == 0 ? 0 : (intval($r['DOLARES']) > 0 ? 1 : 0);
                    
                    if($stat == 1){
                        if($stat == 1 || $stat == NULL){
                            if(intval($r['CHEQUE']) > 0){
                                if($ch['BANCO'] == "BI"){
                                    $iBI++;
                                }else{
                                    $iOtros++;
                                }
                            }
                        }
                    }
                 }
                 
            ?>   
                <tr>
                    <th colspan="4" style="text-align: right">Totales</th>
                    <th style="text-align: right"><?php echo number_format(floatval($totalEfectivo),2)?></th>
                    <th style="text-align: right"><?php echo number_format(floatval($totalBI),2); ?></th>
                    <th style="text-align: right"><?php echo number_format(floatval($totalOtros),2); ?></th>
                    <th style="text-align: right"><?php echo number_format(floatval($totalDeposito),2); ?></th>
                    <th style="text-align: right"><?php echo number_format(floatval($totalDolares),2); ?></th>
                    <th style="text-align: right">&nbsp;</th>
                    <th style="text-align: right">&nbsp;</th>
                    <th style="text-align: right">&nbsp;</th>
                    <th style="text-align: right"><?php echo number_format(floatval($totalFact),2); ?></th>
                    <th>&nbsp;</th>
                </tr>
                <tr>
                    <th colspan="4" style="text-align: right">Facturas</th>
                    <th style="text-align: right"><?php echo $iEfectivo ?></th>
                    <th style="text-align: right"><?php echo $iBI ?></th>
                    <th style="text-align: right"><?php echo $iOtros ?></th>
                    <th style="text-align: right"><?php echo $iDeposito ?></th>
                    <th style="text-align: right"><?php echo $iDolares ?></th>
                    <th style="text-align: right">&nbsp;</th>
                    <th style="text-align: right">&nbsp;</th>
                    <th style="text-align: right">&nbsp;</th>
                    <th style="text-align: right"><?php echo $iFact ?></th>
                    <th>&nbsp;</th>
                </tr>
        </tbody>
        <tfoot>
            <div id="resumendiv">
                <table width="100%" border="0" style="font-size:8px" id="resumen" cellspacing="0" cellpadding="0">
                    <tr>
                        <td width="50%"><table class="table table-striped table-bordered" border="0" cellspacing="0">
                                <tr>
                                    <td colspan="2"><legend style="font-size: 8px">RESUMEN GENERAL</legend></td>   
                                </tr>
                                <tr>
                                    <td>Total Efectivo</td>
                                    <td align="right" class="b">Q.<?=number_format(floatval($totalEfectivo),2)?></td>
                                </tr>
                                <tr>
                                    <td width="55%">Total Cheques BI</td>
                                    <td width="45%" align="right" class="b">Q.<?=number_format(floatval($totalBI),2)?></td>
                                </tr>
                                <tr>
                                    <td>Total Cheques Otros Bancos</td>
                                    <td align="right" class="b">Q.<?=number_format(floatval($totalOtros),2)?></td>
                                </tr>
                                <tr>
                                    <td>Total Depositos Directos</td>
                                    <td align="right" class="b">Q.<?=number_format(floatval($totalDeposito),2)?></td>
                                </tr>
                                <tr class="b">
                                    <td>Total Ingresos Quetzales</td>
                                    <td class="b" align="right">Q.<?=number_format(floatval($totalQ),2)?></td>
                                </tr>
                                <tr>
                                    <td class="b">Total Ingresos Dolares</td>
                                    <td align="right" class="b">&#36;<?=number_format(floatval($totalDolares),2)?></td>
                                </tr>
                                <tr>
                                    <td width="55%">Dolares por Tipo de Cambio</td>
                                    <td align="right" class="b">Q.
                                    <?php $totalSumDol = $tipoCambio*$totalDolares; ?>
                                    <?=number_format(floatval($totalSumDol),2)?></td>
                                </tr>
                                <tr class="b">
                                    <td class="b">Total Ingresos</td>
                                    <td class="b" align="right" >Q.<?=number_format($totalSumDol+$totalQ,2)?></td>
                                </tr>
                                <tr class="b">
                                    <td class="b">Total en Trámite</td>
                                    <td class="b" align="right" >Q.<?=number_format($totalTramite,2)?></td>
                                </tr>
                                <tr class="b">
                                    <td >Total Facturado</td>
                                    <td align="right" class="b">Q.<?=number_format(floatval($totalFact),2)?></td>
                                </tr>
                                <tr>
                                    <td >Diferencia</td>
                                    <td align="right" class="b" >Q.<?=number_format(($totalSumDol+$totalQ)-$totalFact,2)?></td>
                                </tr>
                        </table></td>
                        <td width="50%" valign="top">
                            <div id="firmas">
                                <table width="80%" style="margin-left: 10%" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td colspan="3"><legend style="font-size:10px">Elaborado Por</legend></td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                        <td>&nbsp;</td>
                                    </tr>
                                    <tr>
                                        <td colspan="3"><legend style="font-size:10px">Revisado Por</legend></td>
                                    </tr>
                                </table>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
        
        </tfoot>
    </table>
</div>